why when we build an array like this:
char st[]="abcd";
we have 5 cells in which the last cell is \0
why is that
and what are the number of each cell
is it from 0-4
or 1-5 ??
why when we build an array like this:
char st[]="abcd";
we have 5 cells in which the last cell is \0
why is that
and what are the number of each cell
is it from 0-4
or 1-5 ??
Because at the lowest level, a string in C is just a sequence of characters. And since the length of the string isn't stored anywhere, you need a different way to tell where the end of the string is. So every string in C is terminated by a NULL character (\0), in most cases.
In your example,
you're right that st has five elements, and that the last one is \0. Since C starts counting from zero, these elements will be [0] through [4], and st[4] will be the \0.Code:char st[]="abcd";
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
so every array ends with \0 cell ??
No, every string ends with a '\0', but strings are arrays of characters.so every array ends with \0 cell ??
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thanks i understand now